<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="68px"
    >
      
      <el-form-item label="店员昵称" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入店员昵称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="性别" prop="sex">
        <el-select
          v-model="queryParams.sex"
          placeholder="请选择性别"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.MEMBER_SEX)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
	  <el-form-item label="店员等级" prop="workerClerkLevelId">
		  
		<el-select v-model="queryParams.workerClerkLevelId" placeholder="请选择店员等级" clearable class="!w-240px">
		  <el-option v-for="level in levelOptions" :key="level.id" :label="level.name" :value="level.id">
		    <span class="flex items-center gap-x-8px">
		      <el-avatar :src="level.img" size="small" />
		      {{ level.name }}
		    </span>
		  </el-option>
		</el-select>
	    
	  </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
	<el-tabs v-model="queryParams.onlineStatus" type="card" @tab-click="handleTabClick">
	  <el-tab-pane
	    v-for="item in statusTabs"
	    :key="item.value"
	    :label="item.label"
	    :name="item.value"
	  />
	</el-tabs>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="编号" align="center" prop="id" />
      <el-table-column label="店员昵称" align="center" prop="nickname" />
	  <el-table-column align="center" label="店员头像" prop="avatar" width="80px">
	    <template #default="scope">
	  		  <el-image
	  		    fit="cover"
	  		    :src="scope.row.avatar"
	  		    class="flex-none w-50px h-50px"
	  		    @click="imagePreview(scope.row.avatar)"
	  		  />
	    </template>
	  </el-table-column>
	  <el-table-column label="性别" align="center" prop="sex">
	  		  <template #default="scope">
	  		    <dict-tag :type="DICT_TYPE.MEMBER_SEX" :value="scope.row.sex" />
	  		  </template>
	  </el-table-column>
	  <el-table-column label="等级" align="center" prop="clerkLevel" />
      <el-table-column label="年龄" align="center" prop="age" />
      <el-table-column label="微信" align="center" prop="weixin" width="160px" />
      <el-table-column label="手机号" align="center" prop="mobile" width="160px" />
      <el-table-column label="城市" align="center" prop="city" />
      <el-table-column label="相关经验" align="center" prop="experience" />
      <el-table-column label="录音" align="center"  prop="sound" width="350px">
      	<template #default="scope">
      		<audio v-if="scope.row.sound!=''&&scope.row.sound!=null" :src="scope.row.sound" type="audio/wav"
      			controls="controls"></audio>
      		<span v-else>暂无语音</span>
      	</template>
      </el-table-column>
      <el-table-column label="音频时长" align="center" prop="soundTime" />
	  <el-table-column label="状态" align="center" prop="status">
	  		  <template #default="scope">
	  		  	<el-switch
	  		  	  v-model="scope.row.status"
	  		  	  :active-value="true"
	  		  	  :inactive-value="false"
	  			  @change="handleStatusChange(scope.row)"
	  		  	/>
	  		  </template>
	  </el-table-column>
      <el-table-column label="在线状态" align="center" prop="onlineStatus">
		  <template #default="scope">
		  	<el-switch
		  	  v-model="scope.row.onlineStatus"
		  	  :active-value="true"
		  	  :inactive-value="false"
			  @change="handleOnlineStatusChange(scope.row)"
		  	/>
		  </template>
	  </el-table-column>
	  <el-table-column label="盲盒状态" align="center" prop="blindStatus">
	  		  <template #default="scope">
	  		  	<el-switch
	  		  	  v-model="scope.row.blindStatus"
	  		  	  :active-value="true"
	  		  	  :inactive-value="false"
				  @change="handleBlindStatusChange(scope.row)"
	  		  	/>
	  		  </template>
	  </el-table-column>
	  <el-table-column label="人气店员" align="center" prop="hot">
	  		  <template #default="scope">
	  		  	<el-switch
	  		  	  v-model="scope.row.hot"
	  		  	  :active-value="true"
	  		  	  :inactive-value="false"
				  @change="handleHotChange(scope.row)"
	  		  	/>
	  		  </template>
	  </el-table-column>
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
	  <el-table-column
	    label="更新时间"
	    align="center"
	    prop="updateTime"
	    :formatter="dateFormatter"
	    width="180px"
	  />
      <el-table-column label="操作" align="center" min-width="160px" fixed="right">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['worker:clerk:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['worker:clerk:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <ClerkForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import { TabsPaneContext } from 'element-plus'
import { dateFormatter } from '@/utils/formatTime'
import { cloneDeep } from 'lodash-es'
import { createImageViewer } from '@/components/ImageViewer'
import download from '@/utils/download'
import { ClerkApi, ClerkVO } from '@/api/worker/clerk'
import { ClerkLevelApi, ClerkLevelVO } from '@/api/worker/clerklevel'
import ClerkForm from './ClerkForm.vue'

/** 店员信息 列表 */
defineOptions({ name: 'Clerk' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<ClerkVO[]>([]) // 列表的数据

const statusTabs = ref([
  {
    label: '全部',
    value: 2
  },
  {
    label: '在线',
    value: 1
  },
  {
    label: '休息',
    value: 0
  },
])

const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  userId: undefined,
  nickname: undefined,
  avatar: undefined,
  clerkLevel: undefined,
  sex: undefined,
  age: undefined,
  weixin: undefined,
  mobile: undefined,
  city: undefined,
  experience: undefined,
  sound: undefined,
  soundType: undefined,
  soundTime: [],
  albums: undefined,
  intro: undefined,
  onlineTime: [],
  status: undefined,
  onlineStatus: 2,
  catIds: undefined,
  catList: undefined,
  gameIds: undefined,
  gameList: undefined,
  goodsIds: undefined,
  fans: undefined,
  orderNum: undefined,
  blindStatus: undefined,
  workerClerkLevelId: undefined,
  level: undefined,
  hot: undefined,
  home: undefined,
  skillNum: undefined,
  createTime: [],
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

const levelOptions = ref<ClerkLevelApi.ClerkLevelVO[]>([])

const getClerkLevelList = async () => {
  const data = await ClerkLevelApi.getClerkLevelList()
  levelOptions.value = data;
}

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
	const data = cloneDeep(queryParams)
	// 处理掉全部的状态，不传就是全部
	if (data.onlineStatus === 2) {
		delete data.onlineStatus
	}  
	  
    const res = await ClerkApi.getClerkPage(data)
    list.value = res.list
    total.value = res.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 头像预览 */
const imagePreview = (imgUrl: string) => {
  createImageViewer({
    urlList: [imgUrl]
  })
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 切换 Tab */
const handleTabClick = (tab: TabsPaneContext) => { 
  queryParams.onlineStatus = tab.paneName as number
  getList()
}

/** 修改在线状态 */
const handleOnlineStatusChange = async (row: ClerkApi.ClerkVO) => {
  // 发起修改状态
  await ClerkApi.updateClerkOnlineStatus(row.id, row.onlineStatus)
  // 刷新列表
  await getList()
}

/** 修改状态 */
const handleStatusChange = async (row: ClerkApi.ClerkVO) => {
  // 发起修改状态
  await ClerkApi.updateClerkStatus(row.id, row.status)
  // 刷新列表
  await getList()
}

/** 修改盲盒状态 */
const handleBlindStatusChange = async (row: ClerkApi.ClerkVO) => {
  // 发起修改状态
  await ClerkApi.updateClerkBlindStatus(row.id, row.blindStatus)
  // 刷新列表
  await getList()
}

/** 修改首页推荐 */
const handleHomeChange = async (row: ClerkApi.ClerkVO) => {
  // 发起修改状态
  await ClerkApi.updateClerkHome(row.id, row.home)
  // 刷新列表
  await getList()
}

/** 修改人气店员 */
const handleHotChange = async (row: ClerkApi.ClerkVO) => {
  // 发起修改状态
  await ClerkApi.updateClerkHot(row.id, row.hot)
  // 刷新列表
  await getList()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await ClerkApi.deleteClerk(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await ClerkApi.exportClerk(queryParams)
    download.excel(data, '店员信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
  getClerkLevelList()
})
</script>
